﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace SAP_SnPn
{
    public class SAP_400
    {
        si.iskratel.ntsapcs.service s = new si.iskratel.ntsapcs.service();
        si.iskratel.ntsapcs.ZwsEquiMatDataResponse r;
        si.iskratel.ntsapcs.ZwsEquiMatData p = new si.iskratel.ntsapcs.ZwsEquiMatData();

        // return Serial numbre, input is Process No
        public string getSerial(string Process)
        {
            try
            {
                si.iskratel.ntsapcs.ZstWsEquiMatData podatki = new si.iskratel.ntsapcs.ZstWsEquiMatData();
                podatki.Zsrtiv = Process;
                p.Input = podatki;
                s.Credentials = new System.Net.NetworkCredential("web", "prenos");
                r = s.ZwsEquiMatData(p);

                if (r.Return.Type != "S")
                    throw new System.Exception(r.Return.Message);

                if (r.Output.Length > 0)
                    return r.Output[0].Serge;
                else
                    return null;
            }
            catch
            {
                return null;
            }
        }

        // return Process numbre, input is Serial No
        public string getProcess(string Serial)
        {
            try
            {
                si.iskratel.ntsapcs.ZstWsEquiMatData podatki = new si.iskratel.ntsapcs.ZstWsEquiMatData();
                podatki.Serge = Serial;
                p.Input = podatki;
                s.Credentials = new System.Net.NetworkCredential("web", "prenos");
                r = s.ZwsEquiMatData(p);

                if (r.Return.Type != "S")
                    throw new System.Exception(r.Return.Message);

                if (r.Output.Length > 0)
                    return r.Output[0].Serge;
                else
                    return null;
            }
            catch
            {
                return null;
            }
        }

        // Vrne nalog, vhod je serijska ali procesna
        public string[] getOrder(string Item)
        {
            try
            {

                si.iskratel.ntsapcs.ZstWsEquiMatData podatki_1 = new si.iskratel.ntsapcs.ZstWsEquiMatData();
                si.iskratel.ntsapcs.ZstWsEquiMatData podatki_2 = new si.iskratel.ntsapcs.ZstWsEquiMatData();
                podatki_1.Serge = Item;
                podatki_2.Zsrtiv = Item;

                s.Credentials = new System.Net.NetworkCredential("web", "prenos");
                
                p.Input = podatki_1;
                r = s.ZwsEquiMatData(p);

                if (r.Output.Length > 0)
                    return new string[4] { r.Output[0].Aufnr, r.Output[0].Matnr, r.Output[0].Auart, r.Output[0].Pstat };
                else
                {
                    p.Input = podatki_2;
                    r = s.ZwsEquiMatData(p);

                    if (r.Output.Length > 0)
                        return new string[4] { r.Output[0].Aufnr, r.Output[0].Matnr, r.Output[0].Auart, r.Output[0].Pstat };
                }
                return null;
            }
            catch
            {
                return null;
            }
        }


        // vrne listo serijskih/procesnih. Vhod je nalog
        public List<ArrayList> getData(string nalog)
        {
            si.iskratel.ntsapcs.ZstWsEquiMatData podatki = new si.iskratel.ntsapcs.ZstWsEquiMatData();
            podatki.Aufnr = nalog;
            p.Input = podatki;
            s.Credentials = new System.Net.NetworkCredential("web", "prenos");
            r = s.ZwsEquiMatData(p);

            List<ArrayList> result = new List<ArrayList>();
            ArrayList proc = new ArrayList();
            ArrayList ser = new ArrayList();
            //string[,] x = new string[r.Output.Length, 2];

            foreach (si.iskratel.ntsapcs.ZstWsEquiMatData data in r.Output)
            {
                    proc.Add(data.Serge);
                    if (data.Zsrtiv.Length == 10)
                        ser.Add(data.Zsrtiv.Substring(0, 9));
                    else
                        ser.Add(data.Zsrtiv);
            }

            if (r.Return.Type != "S")
                throw new System.Exception(r.Return.Message);
            result.Add(proc);
            result.Add(ser);
            return result;
            

        }
    }
}
